package com.gastronomy.controller;

import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.gastronomy.entity.DishesEntity;
import com.gastronomy.entity.ReplayInformationEntity;
import com.gastronomy.entity.User;
import com.gastronomy.service.DishesService;
import com.gastronomy.service.ReplayService;
import com.gastronomy.service.UserService;
import com.gastronomy.util.SpringUtil;

@Controller
@RequestMapping("/business")
public class BusinessController {
	
	@Resource
	private DishesService dishesService;
	
	@Resource
	private ReplayService replayService;
	@Resource
	private UserService userService;
	
	@RequestMapping("/home")
	public String toHome(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		System.out.println("进入商家首页");
		String currentPage = request.getParameter("currentPage");
		User user = SpringUtil.getSessionUser(request);
		List<DishesEntity> result = dishesService.listDishes(user,currentPage,12);
		model.addAttribute("dishesEntities",result);
		
		return "/business/businessHome";
	}
	
	
	@RequestMapping("/toAddFood")
	public String toAddFood(HttpServletRequest request,
			HttpServletResponse response, Model model){
		//新增菜肴
		return "/business/addFoodPage";
	}
	
	
	@RequestMapping("/saveFood")
//	@ResponseBody
//	public Map<String, String> saveFood(Model model,
	public String saveFood(Model model,
            HttpServletRequest request,Integer id,  
            MultipartFile picFile,DishesEntity dishesEntity){
		User user = SpringUtil.getSessionUser(request);
		//新增菜肴
		dishesService.saveShesEntity(dishesEntity,picFile,user);
		return "redirect:/business/home";
	}
	
	
	
	@RequestMapping("/replayDis")
	@ResponseBody
	public Map<String, String> replayDis(Model model,
//	public String replayDis(Model model,
            HttpServletRequest request,Integer id,ReplayInformationEntity replayInformationEntity){
		User user = SpringUtil.getSessionUser(request);
//		replayService.addReplay(replayInformationEntity,user);
		
//		return "redirect:/business/home";
		//新增菜肴
		return replayService.addReplay(replayInformationEntity,user);
	}
	
	@RequestMapping("/underShelf")
	@ResponseBody
	public Map<String, String> underShelf(Model model,
            HttpServletRequest request,HttpServletResponse response ){
		Map<String, String> resultMap = new HashMap<String, String>();
		String dishIds = request.getParameter("dishIds");
		return dishesService.underShelf(dishIds);
	}
	
	
	
}
